import { getEventHistory, getEventDict, getEventChinese } from '@/api/event'
export default {
  data() {
    return {
      currentPage: 1,
      pagesize: 20,
      totalDataLength: 0,
      timeRanges: [new Date(new Date().getTime() - 3600 * 1000 * 24), new Date(new Date().getTime())],
      loading: true,
      globalEventTableData: [],
      globalEventTableDataAll: [],
      singleEventTableData: [],
      singleEventTableDataAll: [],
      eventDict: [],
      eventNames: [],
      eventId: 1,
      startTime: '',
      endTime: '',
      queryData: {
        startTime: '',
        endTime: '',
        eventId: 1
      },
      filters: [],
      filter: {
        text: '',
        value: ''
      },
      test: 0,
      tableHeight: 0,
      queryOrPage: ''
    }
  },
  created() {
    this.queryOrPage = 'query'
    this.getEventHistory()
    this.getEventChinese()
  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      // 监听浏览器高度变化，修改表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      }
    })
  },
  updated() {
  },
  computed: {},
  methods: {
    // filterglobalEventChange(column) {
    //   var deviceType = column['deviceType'][0];
    //   if (!deviceType) {
    //     this.globalEventTableData = this.globalEventTableDataAll
    //     this.totalDataLength = this.globalEventTableData.length
    //     return
    //   }
    //   else {
    //     this.globalEventTableData = this.globalEventTableDataAll.filter(function (value) {
    //       return value.deviceType == deviceType
    //     })
    //     this.totalDataLength = this.globalEventTableData.length
    //   }
    // },
    // filterSingleEventChange(column) {
    //   var deviceType = column['deviceType'][0];
    //   if (!deviceType) {
    //     this.singleEventTableData = this.singleEventTableDataAll
    //     this.totalDataLength = this.singleEventTableDataAll.length
    //     return
    //   }
    //   else {
    //     this.singleEventTableData = this.singleEventTableDataAll.filter(function (value) {
    //       return value.deviceType == deviceType
    //     })
    //     this.totalDataLength = this.singleEventTableData.length
    //   }
    // },
    handleCurrentChange: function (currentPage) {
      this.loading = true
      this.currentPage = currentPage;
      this.queryOrPage = 'page'
      this.getEventHistory()
      this.$refs.refsTable.clearFilter()
    },
    handleSizeChange: function (size) {
      this.loading = true
      this.currentPage = 1
      this.pagesize = size
      this.queryOrPage = 'query'
      this.getEventHistory()
      this.$refs.refsTable.clearFilter()
    },
    searchEvents() {
      this.loading = true
      this.currentPage = 1
      this.queryOrPage = 'query'
      this.getEventHistory()
      this.$refs.refsTable.clearFilter()
    },
    async getEventChinese() {
      const res = await getEventChinese()
      this.eventNames = res
    },
    async getEventHistory() {
      this.globalEventTableData = []
      this.singleEventTableData = []
      await this.getEventDict()
      this.queryData.startTime = this.timeRanges[0].getTime()
      this.queryData.endTime = this.timeRanges[1].getTime()
      this.queryData.eventId = this.eventId
      this.queryData.currentPage = this.currentPage
      this.queryData.pagesize = this.pagesize
      this.queryData.queryOrPage = this.queryOrPage
      const res = await getEventHistory(this.queryData)
      if (this.queryOrPage === 'query') {
        this.totalDataLength = Number(res.data.total)
      }
      for (var i = 0; i < res.data.items.length; i++) {
        if (res.data.items[i].TWSEntityLoginExitEvent) {
          this.setLoginExitEvent(res, i, this.eventId)
        }
        else if (res.data.items[i].NotMonitoredLocomotivesAddDelEvent) {
          this.setNotMonitoredLocomotivesAddDelEvent(res, i, this.eventId)
        }
        else if (res.data.items[i].LocationSituationChangeEvent) {
          this.setLocationSituationChangeEvent(res, i, this.eventId)
        }
        else if (res.data.items[i].PrewarnInfoChangeEvent) {
          this.setPrewarnInfoChangeEvent(res, i, this.eventId)
        }
        else if (res.data.items[i].FenceRelationChangeEvent) {
          this.setFenceRelationChangeEvent(res, i, this.eventId)
        }
        else if (res.data.items[i].HandheldConformAlarmEvent) {
          this.setHandheldConformAlarmEvent(res, i, this.eventId)
        }
      }
      this.globalEventTableDataAll = this.globalEventTableData
      this.singleEventTableDataAll = this.singleEventTableData
      if (this.globalEventTableData.length != 0) {
        this.globalEventTableData.sort(
          function (a, b) {
            return a.appearTime < b.appearTime ? 1 : -1
          }
        )
      }
      if (this.singleEventTableData.length != 0) {
        this.singleEventTableData.sort(
          function (a, b) {
            return a.appearTime < b.appearTime ? 1 : -1
          }
        )
      }
      this.loading = false
    },
    async getEventDict() {
      const res = await getEventDict()
      this.eventDict = res
      this.setFilters(res)
    },
    setFilters(res) {
      var filters = []
      for (var key in res.global.deviceType) {
        var filter = { 'text': res.global.deviceType[key], 'value': res.global.deviceType[key] }
        filters.push(filter)
      }
      this.filters = filters
    },
    setLoginExitEvent(res, i, eventId) {
      res.data.items[i].TWSEntityLoginExitEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].TWSEntityLoginExitEvent.deviceType];
      res.data.items[i].TWSEntityLoginExitEvent.eventType = this.eventDict.TWSEntityLoginExitEvent.eventType[res.data.items[i].TWSEntityLoginExitEvent.eventType];
      res.data.items[i].TWSEntityLoginExitEvent.eventReason = this.eventDict.TWSEntityLoginExitEvent.eventReason[res.data.items[i].TWSEntityLoginExitEvent.eventReason];
      if (eventId === 1) {
        var temp = {
          'appearTime': res.data.items[i].TWSEntityLoginExitEvent.appearTime, 'deviceId': res.data.items[i].TWSEntityLoginExitEvent.deviceId,
          'deviceType': res.data.items[i].TWSEntityLoginExitEvent.deviceType, 'descrption': res.data.items[i].TWSEntityLoginExitEvent.eventType + ' ：' + res.data.items[i].TWSEntityLoginExitEvent.eventReason
        }
        this.globalEventTableData.push(temp);
      }
      else {
        this.singleEventTableData.push(res.data.items[i].TWSEntityLoginExitEvent)
      }
    },
    setNotMonitoredLocomotivesAddDelEvent(res, i, eventId) {
      res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceType];
      res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventType = this.eventDict.NotMonitoredLocomotivesAddDelEvent.eventType[res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventType];
      res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventReason = this.eventDict.NotMonitoredLocomotivesAddDelEvent.eventReason[res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventReason];
      // this.globalEventTableData.push(res.data.items[i].NotMonitoredLocomotivesAddDelEvent);
      if (eventId === 1) {
        if (res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventReason) {
          var temp = {
            'appearTime': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.appearTime, 'deviceId': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceId,
            'deviceType': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceType, 'descrption': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventType + '  ：' + res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventReason
          }
          this.globalEventTableData.push(temp);
        }
        else {
          var temp = {
            'appearTime': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.appearTime, 'deviceId': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceId,
            'deviceType': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.deviceType, 'descrption': res.data.items[i].NotMonitoredLocomotivesAddDelEvent.eventType
          }
          this.globalEventTableData.push(temp);
        }
      }
      else {
        this.singleEventTableData.push(res.data.items[i].NotMonitoredLocomotivesAddDelEvent)
      }
    },
    setLocationSituationChangeEvent(res, i, eventId) {
      res.data.items[i].LocationSituationChangeEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].LocationSituationChangeEvent.deviceType];
      res.data.items[i].LocationSituationChangeEvent.statusBeforeChanged = this.eventDict.LocationSituationChangeEvent.statusBeforeChanged[res.data.items[i].LocationSituationChangeEvent.statusBeforeChanged];
      res.data.items[i].LocationSituationChangeEvent.statusAfterChanged = this.eventDict.LocationSituationChangeEvent.statusAfterChanged[res.data.items[i].LocationSituationChangeEvent.statusAfterChanged];
      // this.globalEventTableData.push(res.data.items[i].LocationSituationChangeEvent);
      if (eventId === 1) {
        var temp = {
          'appearTime': res.data.items[i].LocationSituationChangeEvent.appearTime, 'deviceId': res.data.items[i].LocationSituationChangeEvent.deviceId,
          'deviceType': res.data.items[i].LocationSituationChangeEvent.deviceType, 'descrption': '定位状态变更：' + res.data.items[i].LocationSituationChangeEvent.statusBeforeChanged + ' -> ' + res.data.items[i].LocationSituationChangeEvent.statusAfterChanged
        }
        this.globalEventTableData.push(temp);
      }
      else {
        this.singleEventTableData.push(res.data.items[i].LocationSituationChangeEvent)
      }
    },
    setPrewarnInfoChangeEvent(res, i, eventId) {
      res.data.items[i].PrewarnInfoChangeEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].PrewarnInfoChangeEvent.deviceType];
      if (eventId === 1) {
        var descrption = '预警数量变更：' + res.data.items[i].PrewarnInfoChangeEvent.preWarnsNumBeforeChanged + '->' + res.data.items[i].PrewarnInfoChangeEvent.preWarnsNumAfterChanged
        // if (res.data.items[i].PrewarnInfoChangeEvent.preWarnsNumBeforeChanged == 0) {
        //   descrption = '产生预警'
        // }
        // if (res.data.items[i].PrewarnInfoChangeEvent.preWarnsNumAfterChanged == 0) {
        //   descrption = '预警消失'
        // }
        var temp = {
          'appearTime': res.data.items[i].PrewarnInfoChangeEvent.appearTime, 'deviceId': res.data.items[i].PrewarnInfoChangeEvent.deviceId,
          'deviceType': res.data.items[i].PrewarnInfoChangeEvent.deviceType, 'descrption': descrption
        }
        this.globalEventTableData.push(temp);
      }
      else {
        this.singleEventTableData.push(res.data.items[i].PrewarnInfoChangeEvent)
      }
    },
    setFenceRelationChangeEvent(res, i, eventId) {
      res.data.items[i].FenceRelationChangeEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].FenceRelationChangeEvent.deviceType];
      res.data.items[i].FenceRelationChangeEvent.fenceRelationBeforeChanged = this.eventDict.FenceRelationChangeEvent.fenceRelationBeforeChanged[res.data.items[i].FenceRelationChangeEvent.fenceRelationBeforeChanged];
      res.data.items[i].FenceRelationChangeEvent.fenceRelationAfterChanged = this.eventDict.FenceRelationChangeEvent.fenceRelationAfterChanged[res.data.items[i].FenceRelationChangeEvent.fenceRelationAfterChanged];
      if (eventId === 1) {
        var temp = {
          'appearTime': res.data.items[i].FenceRelationChangeEvent.appearTime, 'deviceId': res.data.items[i].FenceRelationChangeEvent.deviceId, 'fence_id': res.data.items[i].FenceRelationChangeEvent.fence_id,
          'deviceType': res.data.items[i].FenceRelationChangeEvent.deviceType, 'descrption': '与围栏关系变更：' + res.data.items[i].FenceRelationChangeEvent.fenceRelationBeforeChanged + ' -> ' + res.data.items[i].FenceRelationChangeEvent.fenceRelationAfterChanged
        }
        this.globalEventTableData.push(temp);
      }
      else {
        this.singleEventTableData.push(res.data.items[i].FenceRelationChangeEvent)
      }
    },
    setHandheldConformAlarmEvent(res, i, eventId) {
      res.data.items[i].HandheldConformAlarmEvent.deviceType = this.eventDict.global.deviceType[res.data.items[i].HandheldConformAlarmEvent.deviceType];
      res.data.items[i].HandheldConformAlarmEvent.type = this.eventDict.HandheldConformAlarmEvent.type[res.data.items[i].HandheldConformAlarmEvent.type];
      res.data.items[i].HandheldConformAlarmEvent.trainType = this.eventDict.HandheldConformAlarmEvent.trainType[res.data.items[i].HandheldConformAlarmEvent.trainType];
      var temp = {
        'appearTime': res.data.items[i].HandheldConformAlarmEvent.appearTime, 'deviceId': res.data.items[i].HandheldConformAlarmEvent.deviceId, 'fence_id': res.data.items[i].HandheldConformAlarmEvent.fence_id,
        'deviceType': res.data.items[i].HandheldConformAlarmEvent.deviceType,
        // 示例：用户在十五秒内点击了与机车D1(id:456;目标点类型:未绑定设备列车)的预警确认按钮(userId:123)
        'descrption': res.data.items[i].HandheldConformAlarmEvent.type + '与机车' + res.data.items[i].HandheldConformAlarmEvent.trainNo
          + ' (id:' + res.data.items[i].HandheldConformAlarmEvent.trainId + ';目标点类型:' + res.data.items[i].HandheldConformAlarmEvent.trainType + ') 的预警确认按钮'
          + (res.data.items[i].HandheldConformAlarmEvent.userId ? ' (userId:' + res.data.items[i].HandheldConformAlarmEvent.userId + ') ' : '')
      }
      if (eventId === 1) {
        this.globalEventTableData.push(temp);
      }
      else {
        this.singleEventTableData.push(temp)
      }
    },
    add0(m) { return m < 10 ? '0' + m : m },
    format(timeStamp) {
      //shijianchuo是整数，否则要parseInt转换
      var time = new Date(timeStamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var y = time.getFullYear();
      var m = time.getMonth() + 1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
    }
  }
}